<!DOCTYPE html><html lang="zh-tw"><head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Generator &#x51FD;&#x6570; | ECMAScript 6&#x5165;&#x95E8;</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.4.3">
        <meta name="author" content="&#x5927;&#x9F3B;&#x5B50;">
        
        <meta name="HandheldFriendly" content="true">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://wohugb.gitbooks.io/ecmascript-6/content/gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="https://gstatic.gitbook.com/images/b1c4c86ca817a71628d2debe73be9a64.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="static/css/style.css">
    
        
        <link rel="stylesheet" href="static/css/website.css">
        
    
    

        
    
    
    <link rel="next" href="promise.html">
    
    
    <link rel="prev" href="iterator.html">
    

        
    
<style>
    .book .book-summary .book-search,
    .book .book-summary ul.summary {
        position: relative;
        top: auto;
        left: auto;
        right: auto;
        bottom: auto;
    }
    .book-summary {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        flex-direction: column;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        overflow-y: auto;
    }
    .book-summary > nav {
        flex: 1 1 auto;
        overflow-y: auto;
        overflow-y: overlay;
    }
    .gl-wrapper {
        margin: 0;
        display: block;
        padding: 0;
        position: relative;
        line-height: 15px;
        background: #fafafa;
    }
    .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        background: -moz-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        top: -24px;
        width: 100%;
        height: 24px;
        content: "";
        display: block;
        position: absolute;
        background: linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
    }
    .book.color-theme-1 .gl-wrapper {
        background: #121212;
    }
    .book.color-theme-1 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: -moz-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
    }
    .book.color-theme-2 .gl-wrapper {
        background: #2e3243;
    }
    .book.color-theme-2 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: -moz-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
    }
    .gl-wrapper .gitbook-link {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        color: #9DAAB6;
        margin: 16px;
        display: flex;
        padding: 0;
        align-items: center;
        margin-left: 32px;
        padding-top: 8px;
        padding-left: 16px;
        border-radius: 3px;
        padding-right: 16px;
        padding-bottom: 8px;
        text-decoration: none;
        background-color: #E6ECF1;
        -webkit-box-align: center;
    }
    .gl-wrapper .tm-logo {
        margin: 0;
        display: block;
        padding: 0;
        font-size: 40px;
    }
    .gl-wrapper .tm-content {
        flex: 1;
        margin: 0;
        display: block;
        padding: 0;
        padding-left: 16px;
    }
    .gl-wrapper .tm-content .container {
        margin: 0;
        display: block;
        padding: 0;
    }
    .gl-wrapper .tm-content .tm-container .tm-text {
        font-size: 12px;
        font-family: "Roboto", sans-serif;
        font-weight: 400;
        line-height: 1.625;
    }
</style></head>
    <body>
        
        
    <div class="book" data-level="11" data-basepath=".." data-revision="Sun Oct 25 2015 02:52:03 GMT+0000 (UTC)">
    

<div class="book-summary">
    <div class="book-search" role="search">
        <input type="text" placeholder="&#x8F38;&#x5165;&#x4E26;&#x641C;&#x5C0B;" class="form-control">
    </div>
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="javascript:;" target="blank" class="custom-link">ECMAScript 6&#x5165;&#x95E8;</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="index.html">
                
                        <i class="fa fa-check"></i>
                        
                        &#x524D;&#x8A00;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/intro.html">
            
                
                    <a href="intro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        &#x7B80;&#x4ECB;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/let.html">
            
                
                    <a href="let.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        let&#x548C;const&#x547D;&#x4EE4;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/destructuring.html">
            
                
                    <a href="destructuring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        &#x53D8;&#x91CF;&#x7684;&#x89E3;&#x6784;&#x8D4B;&#x503C;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/string.html">
            
                
                    <a href="string.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        &#x5B57;&#x7B26;&#x4E32;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/number.html">
            
                
                    <a href="number.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        &#x6570;&#x503C;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/array.html">
            
                
                    <a href="array.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        &#x6570;&#x7EC4;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/object.html">
            
                
                    <a href="object.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        &#x5BF9;&#x8C61;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/function.html">
            
                
                    <a href="function.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        &#x51FD;&#x6570;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/set-map.html">
            
                
                    <a href="set-map.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Set&#x548C;Map&#x6570;&#x636E;&#x7ED3;&#x6784;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="docs/iterator.html">
            
                
                    <a href="iterator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Iterator&#x548C;for...of&#x5FAA;&#x73AF;
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="11" data-path="docs/generator.html">
            
                
                    <a href="">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        Generator &#x51FD;&#x6570;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="docs/promise.html">
            
                
                    <a href="promise.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        Promise&#x5BF9;&#x8C61;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="docs/class.html">
            
                
                    <a href="class.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        Class&#x548C;Module
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="docs/reference.html">
            
                
                    <a href="reference.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        &#x53C2;&#x8003;&#x94FE;&#x63A5;
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            
            
        </ul>
    </nav>

<div class="gl-wrapper">
    <a class="gitbook-link" href="javascript:;" target="blank">
        <div class="tm-logo">
            <svg preserveaspectratio="xMidYMid meet" height="1em" width="1em" fill="currentColor" viewbox="0 0 1067 769" xmlns="http://www.w3.org/2000/svg" stroke="none" class="icon-7f6730be--text-3f89f380"><g><path d="M480.026 640.677c17.205 0 31.2 13.997 31.2 31.194s-13.995 31.193-31.2 31.193c-17.197 0-31.193-13.996-31.193-31.193 0-17.197 13.996-31.194 31.193-31.194m489.93-193.226c-17.203 0-31.2-13.998-31.2-31.195 0-17.204 13.997-31.2 31.2-31.2 17.198 0 31.194 13.996 31.194 31.2 0 17.197-13.996 31.195-31.193 31.195m0-127.804c-53.269 0-96.609 43.34-96.609 96.609 0 10.373 1.723 20.702 5.123 30.741L559.328 616.879c-18.132-26.128-47.521-41.617-79.302-41.617-36.821 0-70.391 21.065-86.63 54.003L106.68 478.109c-30.288-15.927-52.965-65.817-50.56-111.223 1.248-23.687 9.438-42.071 21.897-49.17 7.916-4.493 17.436-4.099 27.526 1.188l1.916 1.01c75.96 40.022 324.6 170.981 335.063 175.844 16.157 7.47 25.14 10.5 52.659-2.547l513.958-267.3c7.53-2.844 16.315-10.062 16.315-21.023 0-15.205-15.72-21.199-15.765-21.199-29.218-14.018-74.163-35.054-117.987-55.57C798.033 84.26 691.861 34.547 645.23 10.132c-40.253-21.072-72.655-3.311-78.432.282l-11.227 5.555C345.727 119.743 64.898 258.826 48.911 268.553 20.278 285.973 2.547 320.679.252 363.768c-3.586 68.304 31.261 139.506 81.069 165.634l303.172 156.354c6.83 47.306 47.55 82.725 95.532 82.725 52.78 0 95.808-42.546 96.603-95.14L910.541 492.38c16.93 13.233 37.92 20.486 59.416 20.486 53.268 0 96.61-43.341 96.61-96.61s-43.342-96.61-96.61-96.61" fill-rule="evenodd"></path></g></svg>
        </div>
        <div class="tm-content">
            <div class="tm-container">
                <span class="tm-text">
                    Powered by <b>GitBook</b>
                </span>
            </div>
        </div>
    </a>
</div></div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="&#x76EE;&#x9304;"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="&#x641C;&#x5C0B;"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5B57;&#x578B;&#x8A2D;&#x5B9A;"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">&#x896F;&#x7DDA;&#x9AD4;</button>
        <button type="button" data-font="1" class="button">&#x7121;&#x896F;&#x7DDA;&#x9AD4;</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">&#x767D;&#x8272;</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">&#x68D5;&#x8910;&#x8272;</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">&#x591C;&#x9593;</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5206;&#x4EAB;"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    &#x5206;&#x4EAB;&#x5230; Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    &#x5206;&#x4EAB;&#x5230; Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    &#x5206;&#x4EAB;&#x5230; Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    &#x5206;&#x4EAB;&#x5230; Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    &#x5206;&#x4EAB;&#x5230; Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="index.html">ECMAScript 6&#x5165;&#x95E8;</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="generator-%E5%87%BD%E6%95%B0">Generator &#x51FD;&#x6570;</h1>
<h2 id="%E8%AF%AD%E6%B3%95">&#x8BED;&#x6CD5;</h2>
<h3 id="%E7%AE%80%E4%BB%8B">&#x7B80;&#x4ECB;</h3>
<p>&#x6240;&#x8C13;Generator&#xFF0C;&#x6709;&#x591A;&#x79CD;&#x7406;&#x89E3;&#x89D2;&#x5EA6;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x7406;&#x89E3;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x7684;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x6BCF;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x53D1;&#x751F;&#x4E00;&#x6B21;&#x6539;&#x53D8;&#xFF08;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x6210;&#x53D1;&#x751F;&#x67D0;&#x4E9B;&#x4E8B;&#x4EF6;&#xFF09;&#x3002;ES6&#x5F15;&#x5165;Generator&#x51FD;&#x6570;&#xFF0C;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x5B8C;&#x5168;&#x63A7;&#x5236;&#x51FD;&#x6570;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x4F9D;&#x6B21;&#x904D;&#x5386;&#x8FD9;&#x4E9B;&#x72B6;&#x6001;&#x3002;</p>
<p>&#x5728;&#x5F62;&#x5F0F;&#x4E0A;&#xFF0C;Generator&#x662F;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x4E24;&#x4E2A;&#x7279;&#x5F81;&#x3002;&#x4E00;&#x662F;&#xFF0C;function&#x547D;&#x4EE4;&#x4E0E;&#x51FD;&#x6570;&#x540D;&#x4E4B;&#x95F4;&#x6709;&#x4E00;&#x4E2A;&#x661F;&#x53F7;&#xFF1B;&#x4E8C;&#x662F;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x90E8;&#x4F7F;&#x7528;yield&#x8BED;&#x53E5;&#xFF0C;&#x5B9A;&#x4E49;&#x904D;&#x5386;&#x5668;&#x7684;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x5373;&#x4E0D;&#x540C;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#xFF08;yield&#x8BED;&#x53E5;&#x5728;&#x82F1;&#x8BED;&#x91CC;&#x7684;&#x610F;&#x601D;&#x5C31;&#x662F;&#x201C;&#x4EA7;&#x51FA;&#x201D;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">helloWorldGenerator</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;hello&apos;</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;world&apos;</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;ending&apos;</span>;
}

<span class="hljs-keyword">var</span> hw = helloWorldGenerator();
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;Generator&#x51FD;&#x6570;helloWorldGenerator&#xFF0C;&#x5B83;&#x7684;&#x904D;&#x5386;&#x5668;&#x6709;&#x4E24;&#x4E2A;&#x6210;&#x5458;&#x201C;hello&#x201D;&#x548C;&#x201C;world&#x201D;&#x3002;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x4F1A;&#x5F97;&#x5230;&#x904D;&#x5386;&#x5668;&#x3002;</p>
<p>&#x5F53;&#x8C03;&#x7528;Generator&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF08;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x6210;&#x6682;&#x505C;&#x6267;&#x884C;&#xFF09;&#x3002;&#x4EE5;&#x540E;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x904D;&#x5386;&#x5668;&#x7684;next&#x65B9;&#x6CD5;&#xFF0C;&#x5C31;&#x4ECE;&#x51FD;&#x6570;&#x4F53;&#x7684;&#x5934;&#x90E8;&#x6216;&#x8005;&#x4E0A;&#x4E00;&#x6B21;&#x505C;&#x4E0B;&#x6765;&#x7684;&#x5730;&#x65B9;&#x5F00;&#x59CB;&#x6267;&#x884C;&#xFF08;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x6210;&#x6062;&#x590D;&#x6267;&#x884C;&#xFF09;&#xFF0C;&#x76F4;&#x5230;&#x9047;&#x5230;&#x4E0B;&#x4E00;&#x4E2A;yield&#x8BED;&#x53E5;&#x4E3A;&#x6B62;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;next&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x5728;&#x904D;&#x5386;yield&#x8BED;&#x53E5;&#x5B9A;&#x4E49;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-javascript">
hw.next() 
<span class="hljs-comment">// { value: &apos;hello&apos;, done: false }</span>

hw.next()
<span class="hljs-comment">// { value: &apos;world&apos;, done: false }</span>

hw.next()
<span class="hljs-comment">// { value: &apos;ending&apos;, done: true }</span>

hw.next()
<span class="hljs-comment">// { value: undefined, done: true }</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E00;&#x5171;&#x8C03;&#x7528;&#x4E86;&#x56DB;&#x6B21;next&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x51FD;&#x6570;&#x5F00;&#x59CB;&#x6267;&#x884C;&#xFF0C;&#x76F4;&#x5230;&#x9047;&#x5230;&#x7B2C;&#x4E00;&#x53E5;yield&#x8BED;&#x53E5;&#x4E3A;&#x6B62;&#x3002;next&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x7684;value&#x5C5E;&#x6027;&#x5C31;&#x662F;&#x5F53;&#x524D;yield&#x8BED;&#x53E5;&#x7684;&#x503C;hello&#xFF0C;done&#x5C5E;&#x6027;&#x7684;&#x503C;false&#xFF0C;&#x8868;&#x793A;&#x904D;&#x5386;&#x8FD8;&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x51FD;&#x6570;&#x4ECE;&#x4E0A;&#x6B21;yield&#x8BED;&#x53E5;&#x505C;&#x4E0B;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x4E00;&#x76F4;&#x6267;&#x884C;&#x5230;&#x4E0B;&#x4E00;&#x4E2A;yield&#x8BED;&#x53E5;&#x3002;next&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x5BF9;&#x8C61;&#x7684;value&#x5C5E;&#x6027;&#x5C31;&#x662F;&#x5F53;&#x524D;yield&#x8BED;&#x53E5;&#x7684;&#x503C;world&#xFF0C;done&#x5C5E;&#x6027;&#x7684;&#x503C;false&#xFF0C;&#x8868;&#x793A;&#x904D;&#x5386;&#x8FD8;&#x6CA1;&#x6709;&#x7ED3;&#x675F;&#x3002;</p>
<p>&#x7B2C;&#x4E09;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x51FD;&#x6570;&#x4ECE;&#x4E0A;&#x6B21;yield&#x8BED;&#x53E5;&#x505C;&#x4E0B;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x4E00;&#x76F4;&#x6267;&#x884C;&#x5230;return&#x8BED;&#x53E5;&#xFF08;&#x5982;&#x679C;&#x6CA1;&#x6709;return&#x8BED;&#x53E5;&#xFF0C;&#x5C31;&#x6267;&#x884C;&#x5230;&#x51FD;&#x6570;&#x7ED3;&#x675F;&#xFF09;&#x3002;next&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x5BF9;&#x8C61;&#x7684;value&#x5C5E;&#x6027;&#xFF0C;&#x5C31;&#x662F;&#x7D27;&#x8DDF;&#x5728;return&#x8BED;&#x53E5;&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF08;&#x5982;&#x679C;&#x6CA1;&#x6709;return&#x8BED;&#x53E5;&#xFF0C;&#x5219;value&#x5C5E;&#x6027;&#x7684;&#x503C;&#x4E3A;undefined&#xFF09;&#xFF0C;done&#x5C5E;&#x6027;&#x7684;&#x503C;true&#xFF0C;&#x8868;&#x793A;&#x904D;&#x5386;&#x5DF2;&#x7ECF;&#x7ED3;&#x675F;&#x3002;</p>
<p>&#x7B2C;&#x56DB;&#x6B21;&#x8C03;&#x7528;&#xFF0C;&#x6B64;&#x65F6;&#x51FD;&#x6570;&#x5DF2;&#x7ECF;&#x8FD0;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;next&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x7684;value&#x5C5E;&#x6027;&#x4E3A;undefined&#xFF0C;done&#x5C5E;&#x6027;&#x4E3A;true&#x3002;&#x4EE5;&#x540E;&#x518D;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F;&#x8FD9;&#x4E2A;&#x503C;&#x3002;</p>
<p>&#x603B;&#x7ED3;&#x4E00;&#x4E0B;&#xFF0C;Generator&#x51FD;&#x6570;&#x4F7F;&#x7528;iterator&#x63A5;&#x53E3;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x7684;iterator&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x6709;&#x7740;value&#x548C;done&#x4E24;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x5176;&#x4E2D;&#xFF0C;value&#x662F;yield&#x8BED;&#x53E5;&#x540E;&#x9762;&#x90A3;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF0C;done&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x662F;&#x5426;&#x904D;&#x5386;&#x7ED3;&#x675F;&#x3002;</p>
<p>&#x7531;&#x4E8E;Generator&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x53EA;&#x6709;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x624D;&#x4F1A;&#x904D;&#x5386;&#x4E0B;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x6240;&#x4EE5;&#x5176;&#x5B9E;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x53EF;&#x4EE5;&#x6682;&#x505C;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x3002;yield&#x8BED;&#x53E5;&#x5C31;&#x662F;&#x6682;&#x505C;&#x6807;&#x5FD7;&#xFF0C;next&#x65B9;&#x6CD5;&#x9047;&#x5230;yield&#xFF0C;&#x5C31;&#x4F1A;&#x6682;&#x505C;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;&#x5C06;&#x7D27;&#x8DDF;&#x5728;yield&#x540E;&#x9762;&#x7684;&#x90A3;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF0C;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x7684;value&#x5C5E;&#x6027;&#x7684;&#x503C;&#x3002;&#x5F53;&#x4E0B;&#x4E00;&#x6B21;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x518D;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;&#xFF0C;&#x76F4;&#x5230;&#x9047;&#x5230;&#x4E0B;&#x4E00;&#x4E2A;yield&#x8BED;&#x53E5;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x518D;&#x9047;&#x5230;&#x65B0;&#x7684;yield&#x8BED;&#x53E5;&#xFF0C;&#x5C31;&#x4E00;&#x76F4;&#x8FD0;&#x884C;&#x5230;&#x51FD;&#x6570;&#x7ED3;&#x675F;&#xFF0C;&#x5C06;return&#x8BED;&#x53E5;&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF0C;&#x4F5C;&#x4E3A;value&#x5C5E;&#x6027;&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x8BE5;&#x51FD;&#x6570;&#x6CA1;&#x6709;return&#x8BED;&#x53E5;&#xFF0C;&#x5219;value&#x5C5E;&#x6027;&#x7684;&#x503C;&#x4E3A;undefined&#x3002;&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x7531;&#x4E8E;yield&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x76F4;&#x5230;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#x624D;&#x4F1A;&#x6267;&#x884C;&#xFF0C;&#x56E0;&#x6B64;&#x7B49;&#x4E8E;&#x4E3A;JavaScript&#x63D0;&#x4F9B;&#x4E86;&#x624B;&#x52A8;&#x7684;&#x201C;&#x60F0;&#x6027;&#x6C42;&#x503C;&#x201D;&#xFF08;Lazy Evaluation&#xFF09;&#x7684;&#x8BED;&#x6CD5;&#x529F;&#x80FD;&#x3002;</p>
<p>yield&#x8BED;&#x53E5;&#x4E0E;return&#x8BED;&#x53E5;&#x6709;&#x70B9;&#x50CF;&#xFF0C;&#x90FD;&#x80FD;&#x8FD4;&#x56DE;&#x7D27;&#x8DDF;&#x5728;&#x8BED;&#x53E5;&#x540E;&#x9762;&#x7684;&#x90A3;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x6BCF;&#x6B21;&#x9047;&#x5230;yield&#xFF0C;&#x51FD;&#x6570;&#x6682;&#x505C;&#x6267;&#x884C;&#xFF0C;&#x4E0B;&#x4E00;&#x6B21;&#x518D;&#x4ECE;&#x8BE5;&#x4F4D;&#x7F6E;&#x7EE7;&#x7EED;&#x5411;&#x540E;&#x6267;&#x884C;&#xFF0C;&#x800C;return&#x8BED;&#x53E5;&#x4E0D;&#x5177;&#x5907;&#x4F4D;&#x7F6E;&#x8BB0;&#x5FC6;&#x7684;&#x529F;&#x80FD;&#x3002;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x91CC;&#x9762;&#xFF0C;&#x53EA;&#x80FD;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF08;&#x6216;&#x8005;&#x8BF4;&#x4E00;&#x4E2A;&#xFF09;return&#x8BED;&#x53E5;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x591A;&#x6B21;&#xFF08;&#x6216;&#x8005;&#x8BF4;&#x591A;&#x4E2A;&#xFF09;yield&#x8BED;&#x53E5;&#x3002;&#x6B63;&#x5E38;&#x51FD;&#x6570;&#x53EA;&#x80FD;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x53EA;&#x80FD;&#x6267;&#x884C;&#x4E00;&#x6B21;return&#xFF1B;Generator&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x53EF;&#x4EE5;&#x6709;&#x4EFB;&#x610F;&#x591A;&#x4E2A;yield&#x3002;&#x4ECE;&#x53E6;&#x4E00;&#x4E2A;&#x89D2;&#x5EA6;&#x770B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8BF4;Generator&#x751F;&#x6210;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x503C;&#xFF0C;&#x8FD9;&#x4E5F;&#x5C31;&#x662F;&#x5B83;&#x7684;&#x540D;&#x79F0;&#x7684;&#x6765;&#x5386;&#xFF08;&#x5728;&#x82F1;&#x8BED;&#x4E2D;&#xFF0C;generator&#x8FD9;&#x4E2A;&#x8BCD;&#x662F;&#x201C;&#x751F;&#x6210;&#x5668;&#x201D;&#x7684;&#x610F;&#x601D;&#xFF09;&#x3002;</p>
<p>Generator&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;yield&#x8BED;&#x53E5;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x53D8;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x5355;&#x7EAF;&#x7684;&#x6682;&#x7F13;&#x6267;&#x884C;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6267;&#x884C;&#x4E86;&#xFF01;&apos;</span>)
}

<span class="hljs-keyword">var</span> generator = f();

setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  generator.next() 
}, <span class="hljs-number">2000</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x51FD;&#x6570;f&#x5982;&#x679C;&#x662F;&#x666E;&#x901A;&#x51FD;&#x6570;&#xFF0C;&#x5728;&#x4E3A;&#x53D8;&#x91CF;generator&#x8D4B;&#x503C;&#x65F6;&#x5C31;&#x4F1A;&#x6267;&#x884C;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x51FD;&#x6570;f&#x662F;&#x4E00;&#x4E2A;Generator&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x53D8;&#x6210;&#x53EA;&#x6709;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x51FD;&#x6570;f&#x624D;&#x4F1A;&#x6267;&#x884C;&#x3002;</p>
<h3 id="next%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%82%E6%95%B0">next&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;</h3>
<p>yield&#x8BED;&#x53E5;&#x672C;&#x8EAB;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x603B;&#x662F;&#x8FD4;&#x56DE;undefined&#x3002;next&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5E26;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x5C31;&#x4F1A;&#x88AB;&#x5F53;&#x4F5C;&#x4E0A;&#x4E00;&#x4E2A;yield&#x8BED;&#x53E5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; <span class="hljs-literal">true</span>; i++) {
    <span class="hljs-keyword">var</span> reset = <span class="hljs-keyword">yield</span> i;
    <span class="hljs-keyword">if</span>(reset) { i = -<span class="hljs-number">1</span>; }
  }
}

<span class="hljs-keyword">var</span> g = f();

g.next() <span class="hljs-comment">// { value: 0, done: false }</span>
g.next() <span class="hljs-comment">// { value: 1, done: false }</span>
g.next(<span class="hljs-literal">true</span>) <span class="hljs-comment">// { value: 0, done: false }</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5148;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x65E0;&#x9650;&#x8FD0;&#x884C;&#x7684;Generator&#x51FD;&#x6570;f&#xFF0C;&#x5982;&#x679C;next&#x65B9;&#x6CD5;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#xFF0C;&#x6BCF;&#x6B21;&#x8FD0;&#x884C;&#x5230;yield&#x8BED;&#x53E5;&#xFF0C;&#x53D8;&#x91CF;reset&#x7684;&#x503C;&#x603B;&#x662F;undefined&#x3002;&#x5F53;next&#x65B9;&#x6CD5;&#x5E26;&#x4E00;&#x4E2A;&#x53C2;&#x6570;true&#x65F6;&#xFF0C;&#x5F53;&#x524D;&#x7684;&#x53D8;&#x91CF;reset&#x5C31;&#x88AB;&#x91CD;&#x7F6E;&#x4E3A;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#xFF08;&#x5373;true&#xFF09;&#xFF0C;&#x56E0;&#x6B64;i&#x4F1A;&#x7B49;&#x4E8E;-1&#xFF0C;&#x4E0B;&#x4E00;&#x8F6E;&#x5FAA;&#x73AF;&#x5C31;&#x4F1A;&#x4ECE;-1&#x5F00;&#x59CB;&#x9012;&#x589E;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x6709;&#x5F88;&#x91CD;&#x8981;&#x7684;&#x8BED;&#x6CD5;&#x610F;&#x4E49;&#x3002;Generator&#x51FD;&#x6570;&#x4ECE;&#x6682;&#x505C;&#x72B6;&#x6001;&#x5230;&#x6062;&#x590D;&#x8FD0;&#x884C;&#xFF0C;&#x5B83;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x72B6;&#x6001;&#xFF08;context&#xFF09;&#x662F;&#x4E0D;&#x53D8;&#x7684;&#x3002;&#x901A;&#x8FC7;next&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5C31;&#x6709;&#x529E;&#x6CD5;&#x5728;Generator&#x51FD;&#x6570;&#x5F00;&#x59CB;&#x8FD0;&#x884C;&#x4E4B;&#x540E;&#xFF0C;&#x7EE7;&#x7EED;&#x5411;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x90E8;&#x6CE8;&#x5165;&#x503C;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;Generator&#x51FD;&#x6570;&#x8FD0;&#x884C;&#x7684;&#x4E0D;&#x540C;&#x9636;&#x6BB5;&#xFF0C;&#x4ECE;&#x5916;&#x90E8;&#x5411;&#x5185;&#x90E8;&#x6CE8;&#x5165;&#x4E0D;&#x540C;&#x7684;&#x503C;&#xFF0C;&#x4ECE;&#x800C;&#x8C03;&#x6574;&#x51FD;&#x6570;&#x884C;&#x4E3A;&#x3002;</p>
<p>&#x518D;&#x770B;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">foo</span>(<span class="hljs-params">x</span>) </span>{
  <span class="hljs-keyword">var</span> y = <span class="hljs-number">2</span> * (<span class="hljs-keyword">yield</span> (x + <span class="hljs-number">1</span>));
  <span class="hljs-keyword">var</span> z = <span class="hljs-keyword">yield</span> (y / <span class="hljs-number">3</span>);
  <span class="hljs-keyword">return</span> (x + y + z);
}

<span class="hljs-keyword">var</span> it = foo(<span class="hljs-number">5</span>);

it.next()
<span class="hljs-comment">// { value:6, done:false }</span>
it.next(<span class="hljs-number">12</span>)
<span class="hljs-comment">// { value:8, done:false }</span>
it.next(<span class="hljs-number">13</span>)
<span class="hljs-comment">// { value:42, done:true }</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x8FD4;&#x56DE;<code>x+1</code>&#x7684;&#x503C;6&#xFF1B;&#x7B2C;&#x4E8C;&#x6B21;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x4E0A;&#x4E00;&#x6B21;yield&#x8BED;&#x53E5;&#x7684;&#x503C;&#x8BBE;&#x4E3A;12&#xFF0C;&#x56E0;&#x6B64;y&#x7B49;&#x4E8E;24&#xFF0C;&#x8FD4;&#x56DE;<code>y / 3</code>&#x7684;&#x503C;8&#xFF1B;&#x7B2C;&#x4E09;&#x6B21;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x4E0A;&#x4E00;&#x6B21;yield&#x8BED;&#x53E5;&#x7684;&#x503C;&#x8BBE;&#x4E3A;13&#xFF0C;&#x56E0;&#x6B64;z&#x7B49;&#x4E8E;13&#xFF0C;&#x8FD9;&#x65F6;x&#x7B49;&#x4E8E;5&#xFF0C;y&#x7B49;&#x4E8E;24&#xFF0C;&#x6240;&#x4EE5;return&#x8BED;&#x53E5;&#x7684;&#x503C;&#x7B49;&#x4E8E;42&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x7531;&#x4E8E;next&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x8868;&#x793A;&#x4E0A;&#x4E00;&#x4E2A;yield&#x8BED;&#x53E5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x4E0D;&#x80FD;&#x5E26;&#x6709;&#x53C2;&#x6570;&#x3002;V8&#x5F15;&#x64CE;&#x76F4;&#x63A5;&#x5FFD;&#x7565;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x53EA;&#x6709;&#x4ECE;&#x7B2C;&#x4E8C;&#x6B21;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#x5F00;&#x59CB;&#xFF0C;&#x53C2;&#x6570;&#x624D;&#x662F;&#x6709;&#x6548;&#x7684;&#x3002;</p>
<h3 id="for-of%E5%BE%AA%E7%8E%AF">for...of&#x5FAA;&#x73AF;</h3>
<p>for...of&#x5FAA;&#x73AF;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x904D;&#x5386;Generator&#x51FD;&#x6570;&#xFF0C;&#x4E14;&#x6B64;&#x65F6;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> *<span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">yield</span> <span class="hljs-number">1</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-number">3</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-number">4</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-number">5</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-number">6</span>;
}

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> v <span class="hljs-keyword">of</span> foo()) {
  <span class="hljs-built_in">console</span>.log(v);
}
<span class="hljs-comment">// 1 2 3 4 5</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F7F;&#x7528;for...of&#x5FAA;&#x73AF;&#xFF0C;&#x4F9D;&#x6B21;&#x663E;&#x793A;5&#x4E2A;yield&#x8BED;&#x53E5;&#x7684;&#x503C;&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x4E00;&#x65E6;next&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x7684;done&#x5C5E;&#x6027;&#x4E3A;true&#xFF0C;for...of&#x5FAA;&#x73AF;&#x5C31;&#x4F1A;&#x4E2D;&#x6B62;&#xFF0C;&#x4E14;&#x4E0D;&#x5305;&#x542B;&#x8BE5;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;return&#x8BED;&#x53E5;&#x8FD4;&#x56DE;&#x7684;6&#xFF0C;&#x4E0D;&#x5305;&#x62EC;&#x5728;for...of&#x5FAA;&#x73AF;&#x4E4B;&#x4E2D;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x5229;&#x7528;generator&#x51FD;&#x6570;&#x548C;for...of&#x5FAA;&#x73AF;&#xFF0C;&#x5B9E;&#x73B0;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">fibonacci</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> [prev, curr] = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>];
    <span class="hljs-keyword">for</span> (;;) {
        [prev, curr] = [curr, prev + curr];
        <span class="hljs-keyword">yield</span> curr;
    }
}

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n <span class="hljs-keyword">of</span> fibonacci()) {
    <span class="hljs-keyword">if</span> (n &gt; <span class="hljs-number">1000</span>) <span class="hljs-keyword">break</span>;
    <span class="hljs-built_in">console</span>.log(n);
}
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x53EF;&#x89C1;&#xFF0C;&#x4F7F;&#x7528;for...of&#x8BED;&#x53E5;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="throw%E6%96%B9%E6%B3%95">throw&#x65B9;&#x6CD5;</h3>
<p>Generator&#x51FD;&#x6570;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x70B9;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5916;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x6355;&#x83B7;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> g = <span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">yield</span>;
        } <span class="hljs-keyword">catch</span> (e) {
            <span class="hljs-keyword">if</span> (e != <span class="hljs-string">&apos;a&apos;</span>) {
                <span class="hljs-keyword">throw</span> e;
            }
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5185;&#x90E8;&#x6355;&#x83B7;&apos;</span>, e);
        }
    }
};

<span class="hljs-keyword">var</span> i = g();
i.next();

<span class="hljs-keyword">try</span> {
    i.throw(<span class="hljs-string">&apos;a&apos;</span>);
    i.throw(<span class="hljs-string">&apos;b&apos;</span>);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5916;&#x90E8;&#x6355;&#x83B7;&apos;</span>, e);
}
<span class="hljs-comment">// &#x5185;&#x90E8;&#x6355;&#x83B7; a</span>
<span class="hljs-comment">// &#x5916;&#x90E8;&#x6355;&#x83B7; b</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x904D;&#x5386;&#x5668;i&#x8FDE;&#x7EED;&#x629B;&#x51FA;&#x4E24;&#x4E2A;&#x9519;&#x8BEF;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x88AB;Generator&#x51FD;&#x6570;&#x4F53;&#x5185;&#x7684;catch&#x6355;&#x83B7;&#xFF0C;&#x7136;&#x540E;Generator&#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x6210;&#xFF0C;&#x4E8E;&#x662F;&#x7B2C;&#x4E8C;&#x4E2A;&#x9519;&#x8BEF;&#x88AB;&#x51FD;&#x6570;&#x4F53;&#x5916;&#x7684;catch&#x6355;&#x83B7;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x5927;&#x5927;&#x65B9;&#x4FBF;&#x4E86;&#x5BF9;&#x9519;&#x8BEF;&#x7684;&#x5904;&#x7406;&#x3002;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x5199;&#x6CD5;&#xFF0C;&#x60F3;&#x8981;&#x6355;&#x83B7;&#x591A;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x5C31;&#x4E0D;&#x5F97;&#x4E0D;&#x4E3A;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x5199;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x8BED;&#x53E5;&#x3002;</p>
<pre><code class="lang-javascript">
foo(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a</span>) </span>{
    <span class="hljs-keyword">if</span> (a.error) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(a.error);
    }

    foo(<span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">b</span>) </span>{
        <span class="hljs-keyword">if</span> (b.error) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(b.error);
        }

        foo(<span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">c</span>) </span>{
            <span class="hljs-keyword">if</span> (c.error) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(c.error);
            }

            <span class="hljs-built_in">console</span>.log(a, b, c);
        });
    });
});
</code></pre>
<p>&#x4F7F;&#x7528;Generator&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5927;&#x5927;&#x7B80;&#x5316;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">g</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">var</span> a = <span class="hljs-keyword">yield</span> foo(<span class="hljs-string">&apos;a&apos;</span>);
        <span class="hljs-keyword">var</span> b = <span class="hljs-keyword">yield</span> foo(<span class="hljs-string">&apos;b&apos;</span>);
        <span class="hljs-keyword">var</span> c = <span class="hljs-keyword">yield</span> foo(<span class="hljs-string">&apos;c&apos;</span>);
    } <span class="hljs-keyword">catch</span> (e) {
        <span class="hljs-built_in">console</span>.log(e);
    }

  <span class="hljs-built_in">console</span>.log(a, b, c);

}
</code></pre>
<h3 id="yield-%E8%AF%AD%E5%8F%A5">yield*&#x8BED;&#x53E5;</h3>
<p>&#x5982;&#x679C;yield&#x547D;&#x4EE4;&#x540E;&#x9762;&#x8DDF;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x9700;&#x8981;&#x5728;yield&#x547D;&#x4EE4;&#x540E;&#x9762;&#x52A0;&#x4E0A;&#x661F;&#x53F7;&#xFF0C;&#x8868;&#x660E;&#x5B83;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#x3002;&#x8FD9;&#x88AB;&#x79F0;&#x4E3A;yield*&#x8BED;&#x53E5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> delegatedIterator = (<span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;Hello!&apos;</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;Bye!&apos;</span>;
}());

<span class="hljs-keyword">let</span> delegatingIterator = (<span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;Greetings!&apos;</span>;
  <span class="hljs-keyword">yield</span>* delegatedIterator;
  <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;Ok, bye.&apos;</span>;
}());

<span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> delegatingIterator) {
  <span class="hljs-built_in">console</span>.log(value);
}
<span class="hljs-comment">// &quot;Greetings!</span>
<span class="hljs-comment">// &quot;Hello!&quot;</span>
<span class="hljs-comment">// &quot;Bye!&quot;</span>
<span class="hljs-comment">// &quot;Ok, bye.&quot;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;delegatingIterator&#x662F;&#x4EE3;&#x7406;&#x8005;&#xFF0C;delegatedIterator&#x662F;&#x88AB;&#x4EE3;&#x7406;&#x8005;&#x3002;&#x7531;&#x4E8E;<code>yield* delegatedIterator</code>&#x8BED;&#x53E5;&#x5F97;&#x5230;&#x7684;&#x503C;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x7528;&#x661F;&#x53F7;&#x8868;&#x793A;&#x3002;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x904D;&#x5386;&#x4E86;&#x591A;&#x4E2A;Genertor&#x51FD;&#x6570;&#xFF0C;&#x6709;&#x9012;&#x5F52;&#x7684;&#x6548;&#x679C;&#x3002;</p>
<p>&#x5982;&#x679C;<code>yield*</code>&#x540E;&#x9762;&#x8DDF;&#x7740;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x5C31;&#x8868;&#x793A;&#x8BE5;&#x6570;&#x7EC4;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x56E0;&#x6B64;&#x5C31;&#x4F1A;&#x904D;&#x5386;&#x6570;&#x7EC4;&#x6210;&#x5458;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">gen</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">yield</span>* [<span class="hljs-string">&quot;a&quot;</span>, <span class="hljs-string">&quot;b&quot;</span>, <span class="hljs-string">&quot;c&quot;</span>];
}

gen().next() <span class="hljs-comment">// { value:&quot;a&quot;, done:false }</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;yield&#x547D;&#x4EE4;&#x540E;&#x9762;&#x5982;&#x679C;&#x4E0D;&#x52A0;&#x661F;&#x53F7;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x6574;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x52A0;&#x4E86;&#x661F;&#x53F7;&#x5C31;&#x8868;&#x793A;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x6570;&#x7EC4;&#x7684;&#x904D;&#x5386;&#x5668;&#x3002;</p>
<p><code>yield*</code>&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x5730;&#x53D6;&#x51FA;&#x5D4C;&#x5957;&#x6570;&#x7EC4;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">iterTree</span>(<span class="hljs-params">tree</span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(tree)) {
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>; i &lt; tree.length; i++) {
      <span class="hljs-keyword">yield</span>* iterTree(tree[i]);
    }
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">yield</span> tree;
  }
}

<span class="hljs-keyword">const</span> tree = [ <span class="hljs-string">&apos;a&apos;</span>, [<span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;c&apos;</span>], [<span class="hljs-string">&apos;d&apos;</span>, <span class="hljs-string">&apos;e&apos;</span>] ];

<span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> x <span class="hljs-keyword">of</span> iterTree(tree)) {
  <span class="hljs-built_in">console</span>.log(x);
}
<span class="hljs-comment">// a</span>
<span class="hljs-comment">// b</span>
<span class="hljs-comment">// c</span>
<span class="hljs-comment">// d</span>
<span class="hljs-comment">// e</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x7A0D;&#x5FAE;&#x590D;&#x6742;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x4F7F;&#x7528;yield*&#x8BED;&#x53E5;&#x904D;&#x5386;&#x5B8C;&#x5168;&#x4E8C;&#x53C9;&#x6811;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// &#x4E0B;&#x9762;&#x662F;&#x4E8C;&#x53C9;&#x6811;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;</span>
<span class="hljs-comment">// &#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x5206;&#x522B;&#x662F;&#x5DE6;&#x6811;&#x3001;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x548C;&#x53F3;&#x6811;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Tree</span>(<span class="hljs-params">left, label, right</span>) </span>{
  <span class="hljs-keyword">this</span>.left = left;
  <span class="hljs-keyword">this</span>.label = label;
  <span class="hljs-keyword">this</span>.right = right;
}

<span class="hljs-comment">// &#x4E0B;&#x9762;&#x662F;&#x4E2D;&#x5E8F;&#xFF08;inorder&#xFF09;&#x904D;&#x5386;&#x51FD;&#x6570;&#x3002;</span>
<span class="hljs-comment">// &#x7531;&#x4E8E;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x7528;generator&#x51FD;&#x6570;&#x3002;</span>
<span class="hljs-comment">// &#x51FD;&#x6570;&#x4F53;&#x5185;&#x91C7;&#x7528;&#x9012;&#x5F52;&#x7B97;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x5DE6;&#x6811;&#x548C;&#x53F3;&#x6811;&#x8981;&#x7528;yield*&#x904D;&#x5386;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">inorder</span>(<span class="hljs-params">t</span>) </span>{
  <span class="hljs-keyword">if</span> (t) {
    <span class="hljs-keyword">yield</span>* inorder(t.left);
    <span class="hljs-keyword">yield</span> t.label;
    <span class="hljs-keyword">yield</span>* inorder(t.right);
  }
}

<span class="hljs-comment">// &#x4E0B;&#x9762;&#x751F;&#x6210;&#x4E8C;&#x53C9;&#x6811;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">make</span>(<span class="hljs-params">array</span>) </span>{
  <span class="hljs-comment">// &#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x53F6;&#x8282;&#x70B9;</span>
  <span class="hljs-keyword">if</span> (array.length == <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Tree(<span class="hljs-literal">null</span>, array[<span class="hljs-number">0</span>], <span class="hljs-literal">null</span>);
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Tree(make(array[<span class="hljs-number">0</span>]), array[<span class="hljs-number">1</span>], make(array[<span class="hljs-number">2</span>]));
}
<span class="hljs-keyword">let</span> tree = make([[[<span class="hljs-string">&apos;a&apos;</span>], <span class="hljs-string">&apos;b&apos;</span>, [<span class="hljs-string">&apos;c&apos;</span>]], <span class="hljs-string">&apos;d&apos;</span>, [[<span class="hljs-string">&apos;e&apos;</span>], <span class="hljs-string">&apos;f&apos;</span>, [<span class="hljs-string">&apos;g&apos;</span>]]]);

<span class="hljs-comment">// &#x904D;&#x5386;&#x4E8C;&#x53C9;&#x6811;</span>
<span class="hljs-keyword">var</span> result = [];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> node <span class="hljs-keyword">of</span> inorder(tree)) {
  result.push(node); 
}

result
<span class="hljs-comment">// [&apos;a&apos;, &apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;, &apos;f&apos;, &apos;g&apos;]</span>
</code></pre>
<h3 id="%E4%BD%9C%E4%B8%BA%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%9A%84generator%E5%87%BD%E6%95%B0">&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x7684;Generator&#x51FD;&#x6570;</h3>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x662F;Generator&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x7B80;&#x5199;&#x6210;&#x4E0B;&#x9762;&#x7684;&#x5F62;&#x5F0F;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> obj = {
  * myGeneratorMethod() {
    &#xB7;&#xB7;&#xB7;
  }
};
</code></pre>
<p>&#x5B83;&#x7684;&#x5B8C;&#x6574;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF0C;&#x4E24;&#x8005;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">let</span> obj = {
  myGeneratorMethod: <span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
    &#xB7;&#xB7;&#xB7;
  }
};
</code></pre>
<h2 id="%E5%90%AB%E4%B9%89">&#x542B;&#x4E49;</h2>
<h3 id="generator%E4%B8%8E%E7%8A%B6%E6%80%81%E6%9C%BA">Generator&#x4E0E;&#x72B6;&#x6001;&#x673A;</h3>
<p>Generator&#x662F;&#x5B9E;&#x73B0;&#x72B6;&#x6001;&#x673A;&#x7684;&#x6700;&#x4F73;&#x7ED3;&#x6784;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;clock&#x51FD;&#x6570;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x673A;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> ticking = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">var</span> clock = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (ticking)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Tick!&apos;</span>);
  <span class="hljs-keyword">else</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Tock!&apos;</span>);
  ticking = !ticking;
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;clock&#x51FD;&#x6570;&#x4E00;&#x5171;&#x6709;&#x4E24;&#x79CD;&#x72B6;&#x6001;&#xFF08;Tick&#x548C;Tock&#xFF09;&#xFF0C;&#x6BCF;&#x8FD0;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x5C31;&#x6539;&#x53D8;&#x4E00;&#x6B21;&#x72B6;&#x6001;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5982;&#x679C;&#x7528;Generator&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> clock = <span class="hljs-function"><span class="hljs-keyword">function</span>*(<span class="hljs-params">_</span>) </span>{
  <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
    <span class="hljs-keyword">yield</span> _;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Tick!&apos;</span>);
    <span class="hljs-keyword">yield</span> _;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Tock!&apos;</span>);
  }
};
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;Generator&#x5B9E;&#x73B0;&#x4E0E;ES5&#x5B9E;&#x73B0;&#x5BF9;&#x6BD4;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5C11;&#x4E86;&#x7528;&#x6765;&#x4FDD;&#x5B58;&#x72B6;&#x6001;&#x7684;&#x5916;&#x90E8;&#x53D8;&#x91CF;ticking&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x66F4;&#x7B80;&#x6D01;&#xFF0C;&#x66F4;&#x5B89;&#x5168;&#xFF08;&#x72B6;&#x6001;&#x4E0D;&#x4F1A;&#x88AB;&#x975E;&#x6CD5;&#x7BE1;&#x6539;&#xFF09;&#x3001;&#x66F4;&#x7B26;&#x5408;&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;&#x7684;&#x601D;&#x60F3;&#xFF0C;&#x5728;&#x5199;&#x6CD5;&#x4E0A;&#x4E5F;&#x66F4;&#x4F18;&#x96C5;&#x3002;Generator&#x4E4B;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x4FDD;&#x5B58;&#x72B6;&#x6001;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x672C;&#x8EAB;&#x5C31;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#xFF0C;&#x5373;&#x76EE;&#x524D;&#x662F;&#x5426;&#x5904;&#x4E8E;&#x6682;&#x505C;&#x6001;&#x3002;</p>
<h3 id="generator%E4%B8%8E%E5%8D%8F%E7%A8%8B">Generator&#x4E0E;&#x534F;&#x7A0B;</h3>
<p>&#x534F;&#x7A0B;&#xFF08;coroutine&#xFF09;&#x662F;&#x4E00;&#x79CD;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x4F20;&#x7EDF;&#x7684;&#x201C;&#x5B50;&#x4F8B;&#x7A0B;&#x201D;&#xFF08;subroutine&#xFF09;&#x91C7;&#x7528;&#x5806;&#x6808;&#x5F0F;&#x201C;&#x540E;&#x8FDB;&#x5148;&#x51FA;&#x201D;&#x7684;&#x6267;&#x884C;&#x65B9;&#x5F0F;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x8C03;&#x7528;&#x7684;&#x5B50;&#x51FD;&#x6570;&#x5B8C;&#x5168;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;&#x624D;&#x4F1A;&#x7ED3;&#x675F;&#x6267;&#x884C;&#x7236;&#x51FD;&#x6570;&#x3002;&#x534F;&#x7A0B;&#x4E0E;&#x5176;&#x4E0D;&#x540C;&#xFF0C;&#x591A;&#x4E2A;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5E76;&#x884C;&#x6267;&#x884C;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5904;&#x4E8E;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x90FD;&#x5904;&#x4E8E;&#x6682;&#x505C;&#x6001;&#xFF08;suspended&#xFF09;&#xFF0C;&#x51FD;&#x6570;&#x4E4B;&#x95F4;&#x53EF;&#x4EE5;&#x4EA4;&#x6362;&#x6267;&#x884C;&#x6743;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6267;&#x884C;&#x5230;&#x4E00;&#x534A;&#xFF0C;&#x53EF;&#x4EE5;&#x6682;&#x505C;&#x6267;&#x884C;&#xFF0C;&#x5C06;&#x6267;&#x884C;&#x6743;&#x4EA4;&#x7ED9;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x7B49;&#x5230;&#x7A0D;&#x540E;&#x6536;&#x56DE;&#x6267;&#x884C;&#x6743;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x518D;&#x6062;&#x590D;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x79CD;&#x53EF;&#x4EE5;&#x5E76;&#x884C;&#x6267;&#x884C;&#x3001;&#x4EA4;&#x6362;&#x6267;&#x884C;&#x6743;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x79F0;&#x4E3A;&#x534F;&#x7A0B;&#x3002;</p>
<p>&#x4E0D;&#x96BE;&#x770B;&#x51FA;&#xFF0C;&#x534F;&#x7A0B;&#x9002;&#x5408;&#x7528;&#x4E8E;&#x591A;&#x4EFB;&#x52A1;&#x8FD0;&#x884C;&#x7684;&#x73AF;&#x5883;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x610F;&#x4E49;&#x4E0A;&#xFF0C;&#x5B83;&#x4E0E;&#x7EBF;&#x7A0B;&#x5F88;&#x76F8;&#x4F3C;&#xFF0C;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x6267;&#x884C;&#x4E0A;&#x4E0B;&#x6587;&#x3001;&#x53EF;&#x4EE5;&#x5206;&#x4EAB;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;&#x5B83;&#x4EEC;&#x7684;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#x5728;&#x4E8E;&#xFF0C;&#x540C;&#x4E00;&#x65F6;&#x95F4;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x5904;&#x4E8E;&#x8FD0;&#x884C;&#x72B6;&#x6001;&#xFF0C;&#x4F46;&#x662F;&#x8FD0;&#x884C;&#x7684;&#x534F;&#x7A0B;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x5176;&#x4ED6;&#x90FD;&#x5904;&#x4E8E;&#x6682;&#x505C;&#x72B6;&#x6001;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x7EBF;&#x7A0B;&#x662F;&#x62A2;&#x5148;&#x5F0F;&#x7684;&#xFF0C;&#x5230;&#x5E95;&#x54EA;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F18;&#x5148;&#x5F97;&#x5230;&#x8D44;&#x6E90;&#xFF0C;&#x5FC5;&#x987B;&#x7531;&#x8FD0;&#x884C;&#x73AF;&#x5883;&#x51B3;&#x5B9A;&#xFF0C;&#x4F46;&#x662F;&#x534F;&#x7A0B;&#x662F;&#x5408;&#x4F5C;&#x5F0F;&#x7684;&#xFF0C;&#x6267;&#x884C;&#x6743;&#x7531;&#x534F;&#x7A0B;&#x81EA;&#x5DF1;&#x5206;&#x914D;&#x3002;&#x4ECE;&#x5B9E;&#x73B0;&#x4E0A;&#x770B;&#xFF0C;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x5B50;&#x4F8B;&#x7A0B;&#x53EA;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x6808;&#xFF08;stack&#xFF09;&#xFF0C;&#x800C;&#x534F;&#x7A0B;&#x662F;&#x540C;&#x65F6;&#x5B58;&#x5728;&#x591A;&#x4E2A;&#x6808;&#xFF0C;&#x4F46;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6808;&#x662F;&#x5728;&#x8FD0;&#x884C;&#x72B6;&#x6001;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x534F;&#x7A0B;&#x662F;&#x4EE5;&#x591A;&#x5360;&#x7528;&#x5185;&#x5B58;&#x4E3A;&#x4EE3;&#x4EF7;&#xFF0C;&#x5B9E;&#x73B0;&#x591A;&#x4EFB;&#x52A1;&#x7684;&#x5E76;&#x884C;&#x3002;</p>
<p>Generator&#x51FD;&#x6570;&#x662F;ECMAScript 6&#x5BF9;&#x534F;&#x7A0B;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x4F46;&#x5C5E;&#x4E8E;&#x4E0D;&#x5B8C;&#x5168;&#x5B9E;&#x73B0;&#xFF0C;&#x53EA;&#x505A;&#x5230;&#x4E86;&#x6682;&#x505C;&#x6267;&#x884C;&#x548C;&#x8F6C;&#x79FB;&#x6267;&#x884C;&#x6743;&#xFF0C;&#x6709;&#x4E00;&#x4E9B;&#x7279;&#x6027;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#xFF0C;&#x6BD4;&#x5982;&#x4E0D;&#x652F;&#x6301;&#x6240;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x4E4B;&#x4E2D;&#x7684;yield&#x8BED;&#x53E5;&#xFF08;&#x5373;&#x9012;&#x5F52;&#x6267;&#x884C;yield&#x8BED;&#x53E5;&#xFF09;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5C06;Generator&#x51FD;&#x6570;&#x770B;&#x4F5C;&#x591A;&#x4EFB;&#x52A1;&#x8FD0;&#x884C;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5B58;&#x5728;&#x591A;&#x4E2A;&#x8FDB;&#x5165;&#x70B9;&#x548C;&#x9000;&#x51FA;&#x70B9;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x5E76;&#x53D1;&#x7684;&#x591A;&#x4EFB;&#x52A1;&#x53EF;&#x4EE5;&#x5199;&#x6210;&#x591A;&#x4E2A;Generator&#x51FD;&#x6570;&#xFF1B;&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x7EE7;&#x53D1;&#x7684;&#x4EFB;&#x52A1;&#x5219;&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x53D1;&#x751F;&#x987A;&#x5E8F;&#xFF0C;&#x5199;&#x5728;&#x4E00;&#x4E2A;Generator&#x51FD;&#x6570;&#x4E4B;&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x7528;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7BA1;&#x7406;&#x51FD;&#x6570;&#x6267;&#x884C;&#xFF08;&#x53C2;&#x8003;&#x672C;&#x8282;&#x7684;&#x201C;&#x63A7;&#x5236;&#x6D41;&#x7BA1;&#x7406;&#x201D;&#x90E8;&#x5206;&#xFF09;&#x3002;</p>
<h2 id="%E5%BA%94%E7%94%A8">&#x5E94;&#x7528;</h2>
<p>Generator&#x53EF;&#x4EE5;&#x6682;&#x505C;&#x51FD;&#x6570;&#x6267;&#x884C;&#xFF0C;&#x8FD4;&#x56DE;&#x4EFB;&#x610F;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x8FD9;&#x79CD;&#x7279;&#x70B9;&#x4F7F;&#x5F97;Generator&#x6709;&#x591A;&#x79CD;&#x5E94;&#x7528;&#x573A;&#x666F;&#x3002;</p>
<h3 id="%EF%BC%881%EF%BC%89%E5%BC%82%E6%AD%A5%E6%93%8D%E4%BD%9C%E7%9A%84%E5%90%8C%E6%AD%A5%E5%8C%96%E8%A1%A8%E8%BE%BE">&#xFF08;1&#xFF09;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7684;&#x540C;&#x6B65;&#x5316;&#x8868;&#x8FBE;</h3>
<p>Generator&#x51FD;&#x6570;&#x7684;&#x6682;&#x505C;&#x6267;&#x884C;&#x7684;&#x6548;&#x679C;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x53EF;&#x4EE5;&#x628A;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x5199;&#x5728;yield&#x8BED;&#x53E5;&#x91CC;&#x9762;&#xFF0C;&#x7B49;&#x5230;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#x518D;&#x5F80;&#x540E;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x5B9E;&#x9645;&#x4E0A;&#x7B49;&#x540C;&#x4E8E;&#x4E0D;&#x9700;&#x8981;&#x5199;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7684;&#x540E;&#x7EED;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x653E;&#x5728;yield&#x8BED;&#x53E5;&#x4E0B;&#x9762;&#xFF0C;&#x53CD;&#x6B63;&#x8981;&#x7B49;&#x5230;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#x518D;&#x6267;&#x884C;&#x3002;&#x6240;&#x4EE5;&#xFF0C;Generator&#x51FD;&#x6570;&#x7684;&#x4E00;&#x4E2A;&#x91CD;&#x8981;&#x5B9E;&#x9645;&#x610F;&#x4E49;&#x5C31;&#x662F;&#x7528;&#x6765;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x6539;&#x5199;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">loadUI</span>(<span class="hljs-params"></span>) </span>{ 
    showLoadingScreen(); 
    <span class="hljs-keyword">yield</span> loadUIDataAsynchronously(); 
    hideLoadingScreen(); 
} 
<span class="hljs-keyword">var</span> loader = loadUI();
<span class="hljs-comment">// &#x52A0;&#x8F7D;UI</span>
loader.next() 

<span class="hljs-comment">// &#x5378;&#x8F7D;UI</span>
loader.next()
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x8868;&#x793A;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;loadUI&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#xFF0C;&#x4EC5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x904D;&#x5386;&#x5668;&#x3002;&#x4E0B;&#x4E00;&#x6B21;&#x5BF9;&#x8BE5;&#x904D;&#x5386;&#x5668;&#x8C03;&#x7528;next&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x663E;&#x793A;Loading&#x754C;&#x9762;&#xFF0C;&#x5E76;&#x4E14;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x3002;&#x7B49;&#x5230;&#x6570;&#x636E;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#xFF0C;&#x518D;&#x4E00;&#x6B21;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4F1A;&#x9690;&#x85CF;Loading&#x754C;&#x9762;&#x3002;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#x7684;&#x597D;&#x5904;&#x662F;&#x6240;&#x6709;Loading&#x754C;&#x9762;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x90FD;&#x88AB;&#x5C01;&#x88C5;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6309;&#x90E8;&#x5C31;&#x73ED;&#x975E;&#x5E38;&#x6E05;&#x6670;&#x3002;</p>
<p>Ajax&#x662F;&#x5178;&#x578B;&#x7684;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x901A;&#x8FC7;Generator&#x51FD;&#x6570;&#x90E8;&#x7F72;Ajax&#x64CD;&#x4F5C;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x540C;&#x6B65;&#x7684;&#x65B9;&#x5F0F;&#x8868;&#x8FBE;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> result = <span class="hljs-keyword">yield</span> request(<span class="hljs-string">&quot;http://some.url&quot;</span>);
  <span class="hljs-keyword">var</span> resp = <span class="hljs-built_in">JSON</span>.parse(result);
    <span class="hljs-built_in">console</span>.log(resp.value);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">request</span>(<span class="hljs-params">url</span>) </span>{
  makeAjaxCall(url, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>)</span>{
    it.next(response);
  });
}

<span class="hljs-keyword">var</span> it = main();
it.next();
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;main&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x662F;&#x901A;&#x8FC7;Ajax&#x64CD;&#x4F5C;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x9664;&#x4E86;&#x591A;&#x4E86;&#x4E00;&#x4E2A;yield&#xFF0C;&#x5B83;&#x51E0;&#x4E4E;&#x4E0E;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#x7684;&#x5199;&#x6CD5;&#x5B8C;&#x5168;&#x4E00;&#x6837;&#x3002;&#x6CE8;&#x610F;&#xFF0C;makeAjaxCall&#x51FD;&#x6570;&#x4E2D;&#x7684;next&#x65B9;&#x6CD5;&#xFF0C;&#x5FC5;&#x987B;&#x52A0;&#x4E0A;response&#x53C2;&#x6570;&#xFF0C;&#x56E0;&#x4E3A;yield&#x8BED;&#x53E5;&#x6784;&#x6210;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x672C;&#x8EAB;&#x662F;&#x6CA1;&#x6709;&#x503C;&#x7684;&#xFF0C;&#x603B;&#x662F;&#x7B49;&#x4E8E;undefined&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x901A;&#x8FC7;Generator&#x51FD;&#x6570;&#x9010;&#x884C;&#x8BFB;&#x53D6;&#x6587;&#x672C;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">numbers</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> file = <span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">&quot;numbers.txt&quot;</span>);
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">while</span>(!file.eof) {
            <span class="hljs-keyword">yield</span> <span class="hljs-built_in">parseInt</span>(file.readLine(), <span class="hljs-number">10</span>);
        }
    } <span class="hljs-keyword">finally</span> {
        file.close();
    }
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6253;&#x5F00;&#x6587;&#x672C;&#x6587;&#x4EF6;&#xFF0C;&#x4F7F;&#x7528;yield&#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x9010;&#x884C;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x3002;</p>
<h3 id="%EF%BC%882%EF%BC%89%E6%8E%A7%E5%88%B6%E6%B5%81%E7%AE%A1%E7%90%86">&#xFF08;2&#xFF09;&#x63A7;&#x5236;&#x6D41;&#x7BA1;&#x7406;</h3>
<p>&#x5982;&#x679C;&#x6709;&#x4E00;&#x4E2A;&#x591A;&#x6B65;&#x64CD;&#x4F5C;&#x975E;&#x5E38;&#x8017;&#x65F6;&#xFF0C;&#x91C7;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x5199;&#x6210;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<pre><code class="lang-javascript">
step1(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value1</span>) </span>{
  step2(value1, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value2</span>) </span>{
    step3(value2, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value3</span>) </span>{
      step4(value3, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">value4</span>) </span>{
        <span class="hljs-comment">// Do something with value4</span>
      });
    });
  });
});
</code></pre>
<p>&#x91C7;&#x7528;Promise&#x6539;&#x5199;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-javascript">
Q.fcall(step1)
.then(step2)
.then(step3)
.then(step4)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value4</span>) </span>{
    <span class="hljs-comment">// Do something with value4</span>
}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-comment">// Handle any error from step1 through step4</span>
})
.done();
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x628A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x6539;&#x6210;&#x4E86;&#x76F4;&#x7EBF;&#x6267;&#x884C;&#x7684;&#x5F62;&#x5F0F;&#x3002;Generator&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8FDB;&#x4E00;&#x6B65;&#x6539;&#x5584;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x6D41;&#x7A0B;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">longRunningTask</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {    
    <span class="hljs-keyword">var</span> value1 = <span class="hljs-keyword">yield</span> step1();
    <span class="hljs-keyword">var</span> value2 = <span class="hljs-keyword">yield</span> step2(value1);
    <span class="hljs-keyword">var</span> value3 = <span class="hljs-keyword">yield</span> step3(value2);
    <span class="hljs-keyword">var</span> value4 = <span class="hljs-keyword">yield</span> step4(value3);
    <span class="hljs-comment">// Do something with value4</span>
  } <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-comment">// Handle any error from step1 through step4</span>
  }
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6309;&#x6B21;&#x5E8F;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x6240;&#x6709;&#x6B65;&#x9AA4;&#x3002;</p>
<pre><code class="lang-javascript">
scheduler(longRunningTask());

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">scheduler</span>(<span class="hljs-params">task</span>) </span>{
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">if</span> (!task.next(task.value).done) {
            scheduler(task);
    }
  }, <span class="hljs-number">0</span>);
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;yield&#x8BED;&#x53E5;&#x662F;&#x540C;&#x6B65;&#x8FD0;&#x884C;&#xFF0C;&#x4E0D;&#x662F;&#x5F02;&#x6B65;&#x8FD0;&#x884C;&#xFF08;&#x5426;&#x5219;&#x5C31;&#x5931;&#x53BB;&#x4E86;&#x53D6;&#x4EE3;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x8BBE;&#x8BA1;&#x76EE;&#x7684;&#x4E86;&#xFF09;&#x3002;&#x5B9E;&#x9645;&#x64CD;&#x4F5C;&#x4E2D;&#xFF0C;&#x4E00;&#x822C;&#x8BA9;yield&#x8BED;&#x53E5;&#x8FD4;&#x56DE;Promise&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> Q = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;q&apos;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delay</span>(<span class="hljs-params">milliseconds</span>) </span>{
  <span class="hljs-keyword">var</span> deferred = Q.defer();
  setTimeout(deferred.resolve, milliseconds);
  <span class="hljs-keyword">return</span> deferred.promise;
}

<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">f</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">yield</span> delay(<span class="hljs-number">100</span>);
};
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F7F;&#x7528;Promise&#x7684;&#x51FD;&#x6570;&#x5E93;Q&#xFF0C;yield&#x8BED;&#x53E5;&#x8FD4;&#x56DE;&#x7684;&#x5C31;&#x662F;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#x3002;</p>
<h3 id="%EF%BC%883%EF%BC%89%E9%83%A8%E7%BD%B2iterator%E6%8E%A5%E5%8F%A3">&#xFF08;3&#xFF09;&#x90E8;&#x7F72;iterator&#x63A5;&#x53E3;</h3>
<p>&#x5229;&#x7528;Generator&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;&#x4E0A;&#x90E8;&#x7F72;iterator&#x63A5;&#x53E3;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">iterEntries</span>(<span class="hljs-params">obj</span>) </span>{
    <span class="hljs-keyword">let</span> keys = <span class="hljs-built_in">Object</span>.keys(obj);
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>; i &lt; keys.length; i++) {
        <span class="hljs-keyword">let</span> key = keys[i];
        <span class="hljs-keyword">yield</span> [key, obj[key]];
    }
}

<span class="hljs-keyword">let</span> myObj = { foo: <span class="hljs-number">3</span>, bar: <span class="hljs-number">7</span> };

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> [key, value] <span class="hljs-keyword">of</span> iterEntries(myObj)) {
    <span class="hljs-built_in">console</span>.log(key, value);
}

<span class="hljs-comment">// foo 3</span>
<span class="hljs-comment">// bar 7</span>
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;myObj&#x662F;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#xFF0C;&#x901A;&#x8FC7;iterEntries&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x6709;&#x4E86;iterator&#x63A5;&#x53E3;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;&#x4E0A;&#x90E8;&#x7F72;next&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x6570;&#x7EC4;&#x90E8;&#x7F72;Iterator&#x63A5;&#x53E3;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5C3D;&#x7BA1;&#x6570;&#x7EC4;&#x539F;&#x751F;&#x5177;&#x6709;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">makeSimpleGenerator</span>(<span class="hljs-params">array</span>)</span>{
  <span class="hljs-keyword">var</span> nextIndex = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">while</span>(nextIndex &lt; array.length){
    <span class="hljs-keyword">yield</span> array[nextIndex++];
  }
}

<span class="hljs-keyword">var</span> gen = makeSimpleGenerator([<span class="hljs-string">&apos;yo&apos;</span>, <span class="hljs-string">&apos;ya&apos;</span>]);

gen.next().value <span class="hljs-comment">// &apos;yo&apos;</span>
gen.next().value <span class="hljs-comment">// &apos;ya&apos;</span>
gen.next().done  <span class="hljs-comment">// true</span>
</code></pre>
<h3 id="%EF%BC%884%EF%BC%89%E4%BD%9C%E4%B8%BA%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84">&#xFF08;4&#xFF09;&#x4F5C;&#x4E3A;&#x6570;&#x636E;&#x7ED3;&#x6784;</h3>
<p>Generator&#x53EF;&#x4EE5;&#x770B;&#x4F5C;&#x662F;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x66F4;&#x786E;&#x5207;&#x5730;&#x8BF4;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x4F5C;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x7ED3;&#x6784;&#xFF0C;&#x56E0;&#x4E3A;Generator&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x503C;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5B83;&#x53EF;&#x4EE5;&#x5BF9;&#x4EFB;&#x610F;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x63D0;&#x4F9B;&#x7C7B;&#x4F3C;&#x6570;&#x7EC4;&#x7684;&#x63A5;&#x53E3;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> *<span class="hljs-title">doStuff</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">yield</span> fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;hello.txt&apos;</span>);
  <span class="hljs-keyword">yield</span> fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;world.txt&apos;</span>);
  <span class="hljs-keyword">yield</span> fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;and-such.txt&apos;</span>);
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x4F9D;&#x6B21;&#x8FD4;&#x56DE;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x7531;&#x4E8E;&#x4F7F;&#x7528;&#x4E86;Generator&#x51FD;&#x6570;&#xFF0C;&#x5BFC;&#x81F4;&#x53EF;&#x4EE5;&#x50CF;&#x5904;&#x7406;&#x6570;&#x7EC4;&#x90A3;&#x6837;&#xFF0C;&#x5904;&#x7406;&#x8FD9;&#x4E09;&#x4E2A;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">for</span> (task <span class="hljs-keyword">of</span> doStuff()) {
  <span class="hljs-comment">// task&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x50CF;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x90A3;&#x6837;&#x4F7F;&#x7528;&#x5B83;</span>
}
</code></pre>
<p>&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x5982;&#x679C;&#x7528;ES5&#x8868;&#x8FBE;&#xFF0C;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x7528;&#x6570;&#x7EC4;&#x6A21;&#x62DF;Generator&#x7684;&#x8FD9;&#x79CD;&#x7528;&#x6CD5;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doStuff</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> [
    fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;hello.txt&apos;</span>),
    fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;world.txt&apos;</span>),
    fs.readFile.bind(<span class="hljs-literal">null</span>, <span class="hljs-string">&apos;and-such.txt&apos;</span>)
  ];
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#x7684;for...of&#x5FAA;&#x73AF;&#x5904;&#x7406;&#xFF01;&#x4E24;&#x76F8;&#x4E00;&#x6BD4;&#x8F83;&#xFF0C;&#x5C31;&#x4E0D;&#x96BE;&#x770B;&#x51FA;Generator&#x4F7F;&#x5F97;&#x6570;&#x636E;&#x6216;&#x8005;&#x64CD;&#x4F5C;&#xFF0C;&#x5177;&#x5907;&#x4E86;&#x7C7B;&#x4F3C;&#x6570;&#x7EC4;&#x7684;&#x63A5;&#x53E3;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="iterator.html" class="navigation navigation-prev " aria-label="Previous page: Iterator&#x548C;for...of&#x5FAA;&#x73AF;"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="promise.html" class="navigation navigation-next " aria-label="Next page: Promise&#x5BF9;&#x8C61;"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="static/js/app.js"></script>

    
    <script src="static/js/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"disqus":{"shortName":"git-book"}};
    gitbook.start(config);
});
</script>

        
    
    

</body></html>